﻿using System;

namespace Union_Find
{
    public class UFV1 : IUF
    {
        private int[] id;
        private int count;
        public UFV1(int n)
        {
            count = n;
            id = new int[n];
            for (int i = 0; i < n; i++) id[i] = i;
        }

        public void Union(int p, int q)
        {
            int pid = Find(p);
            int qid = Find(q);

            if (pid == qid) return;

            for (int i = 0; i < id.Length; i++)
            {
                if (id[i] == pid) id[i] = qid;
            }

            count--;
        }

        public int Find(int p)
        {
            return id[p];
        }

        public bool Connected(int p, int q)
        {
            return Find(p) == Find(q);
        }

        public int Count => count;
    }
}
